package com.hc.core.jdbc;

import java.beans.PropertyDescriptor;

import com.hc.core.Constants;
import com.hc.core.util.PropertiesTool;
import com.hc.core.util.StringUtil;

public class BaseDao {
	/**
	 * 根据表名生成sql语句
	 * 
	 */
	public String createInsertSqlForTableName(String tableName) {
		String columnNameString = PropertiesTool.getInstance()
				.getPropertiesValue(tableName);

		StringBuffer insertSql = new StringBuffer();
		insertSql.append("insert into ");
		insertSql.append(tableName);
		insertSql.append("(");
		insertSql.append(columnNameString);
		insertSql.append(") ");
		insertSql.append("values ");
		insertSql.append("(");
		insertSql.append(createColumnQuestionValue(columnNameString));
		insertSql.append(") ");
		return insertSql.toString();
	}

	/**
	 * 生成列值
	 * 
	 * @param tableName
	 * @param object
	 * @param objectClass
	 * @return
	 */
	public Object[] getColumnValue(String tableName, Class objectClass,
			Object object) {
		String[] columnName = StringUtil.split(PropertiesTool.getInstance()
				.getPropertiesValue(tableName), Constants.char_comma);
		Object[] columnValue = new Object[columnName.length];
		for (int i = 0; null != columnName && i < columnName.length; i++) {
			try {
				PropertyDescriptor pd = new PropertyDescriptor(columnName[i],
						objectClass);
				columnValue[i] = pd.getReadMethod().invoke(object);
				System.out.println(columnName[i] + "==>" + columnValue[i]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return columnValue;
	}

	/**
	 * 生成问号列值
	 * 
	 * @param columnName
	 * @return
	 */
	private String createColumnQuestionValue(String columnName) {
		String[] columnArray = columnName.split(Constants.char_comma);
		StringBuffer columnBuffer = new StringBuffer();
		for (int i = 0; null != columnArray && i < columnArray.length; i++) {
			columnBuffer.append(Constants.char_question_mark);
			if (i + 1 != columnArray.length)
				columnBuffer.append(",");
		}
		return columnBuffer.toString();
	}
	
	/***
	 * 生成
	 * @param tableName
	 * @param condition
	 * @param orderBy
	 * @return
	 */
	public String createSelectSqlForCondition(String tableName,
			String[] condition,String orderBy) {
		StringBuffer selectSql = new StringBuffer();
		selectSql.append("select ");
		selectSql.append(PropertiesTool.getInstance().getPropertiesValue(
				tableName)
				+ " ");
		selectSql.append("from ");
		selectSql.append(tableName + " ");
		if (null != condition && condition.length > 0) {
			selectSql.append("where ");
			for (int i = 0; i < condition.length; i++) {
				selectSql.append(condition[i]);
				if (i + 1 != condition.length)
					selectSql.append(" and ");
			}
		}
		if(null != orderBy && orderBy.length() > 0){
			selectSql.append(" order by " + orderBy);
		}
		return selectSql.toString();
	}

	/**
	 * 根据表名以及条件生成查询语句
	 * 
	 * @param tableName
	 * @return
	 */
	public String createSelectSqlForCondition(String tableName,
			String[] condition) {
		StringBuffer selectSql = new StringBuffer();
		selectSql.append("select ");
		selectSql.append(PropertiesTool.getInstance().getPropertiesValue(
				tableName)
				+ " ");
		selectSql.append("from ");
		selectSql.append(tableName + " ");
		if (null != condition && condition.length > 0) {
			selectSql.append("where ");
			for (int i = 0; i < condition.length; i++) {
				selectSql.append(condition[i]);
				if (i + 1 != condition.length)
					selectSql.append(" and ");
			}
		}
		return selectSql.toString();
	}

	/**
	 * 创建删除语句
	 * 
	 * @param tableName
	 * @param condition
	 * @return
	 */
	public String createDeleteSqlForCondition(String tableName,
			String[] condition) {
		StringBuffer deleteSql = new StringBuffer();
		deleteSql.append("delete ");
		deleteSql.append(tableName);
		deleteSql.append("from ");
		deleteSql.append(tableName + " ");
		if (null != condition && condition.length > 0) {
			deleteSql.append("where ");
			for (int i = 0; i < condition.length; i++) {
				deleteSql.append(condition[i]);
				if (i + 1 != condition.length)
					deleteSql.append(" and ");
			}
		}
		return deleteSql.toString();
	}

	/**
	 * 生成log
	 * 
	 * @param sql
	 * @param time
	 * @return
	 */
	public String getSqlExecuteTimeLog(String sql, long time) {
		StringBuffer temp = new StringBuffer();
		temp.append(sql);
		temp.append(" --- ");
		temp.append(time);
		temp.append("毫秒");
		return temp.toString();
	}

	/**
	 * 
	 * @param sql
	 * @return
	 */
	public String getSqlExcuteErrorLog(String sql) {
		StringBuffer temp = new StringBuffer();
		temp.append("执行");
		temp.append(sql);
		temp.append("出现异常!原因如下:");
		return temp.toString();
	}
}
